<?php

class FriendsController extends Zend_Controller_Action
{
	protected $_session;
		
    public function init()
    {
        $storage = new Zend_Auth_Storage_Session();
		$session = $storage->read();
		//there is no data in session, then direct to login action
		if(!$session){
			$this->_redirect('auth/login');
		}
		//there is data in session, show the user's information
		$this->_session = $session;
    }

    public function indexAction()
    {
        // action body
    }

    public function addfriendsAction()
    {
    	$storage = new Zend_Auth_Storage_Session();
		$session = $storage->read();
    	$founderId = $session->user_id;
    	
        $usersMapper = new Model_UsersMapper();
        $user = new Model_Users();
        $friendsMapper = new Model_FriendsMapper();
        $friends = new Model_Friends();
        
        //get the target id.
        $targetId = $_REQUEST['target_id'];
        $message = $_REQUEST['message'];
        
        if(!$targetId){
        	//if it is not add by user id, then get the account
        	$account = $_REQUEST['account'];
        	if(!$account){
                $this->_helper->json('Please enter the account of your friends.');
                return;
        	} else {
        		//get the user id by account
                $usersMapper->findUserByAccount($account, $user);
                if(empty($user)){
                	$this->_helper->json('The account you input is not existed.');
                	return;
                }
                
                $targetId = $user->getUser_id();
        	}
        } else {
        	$usersMapper->find($targetId, $user);
        }
        //check whether the data has existing in the database, if yes, return failure.
        $test = $friendsMapper->chechUnique($founderId, $targetId);
        if($friendsMapper->chechUnique($founderId, $targetId)){
            $this->_helper->json('He/She is already your friend or the request has already send.');
           	return;
        }
                
        $friends->setFounder_id($founderId);
        $friends->setFounder_name($session->name);
        $friends->setTarget_id($targetId);
        $friends->setTarget_name($user->getName());
        $friends->setRequest_message($message);
        $friends->setStatues("W");
       	//insert the friends data to data base.
       	try{
        	$friendsMapper->save($friends);
        	$this->_helper->json('The request has send to your friend.');
        	return;
       	} catch (Exception $e){
       		$this->_helper->json('Add friend request failed.');
       		return;
       	}
    }

    public function confirmfriendsAction()
    {
    	$storage = new Zend_Auth_Storage_Session();
       	$session = $storage->read();
        $userId = $session->user_id;
                                		
        $friendsMapper = new Model_FriendsMapper();
        $friends = new Model_Friends();
                                		
        //get the data id from view.
        $friendId = $_REQUEST['friend_id'];
                                		
        //get the record by id and check if the user have permission to confirm
        $friendsMapper->find($friendId, $friends);
        if($userId == $friends->getTarget_id()){
        	$friends->setStatues("C");
        	try {
	            $friendsMapper->save($friends);
	            $this->_helper->json("He/She is your friend.");
	            return;
        	} catch (Exception $e){
        		$this->_helper->json("confirm failed.");
	            return;
        	}
        } else {
        	$this->_helper->json("Confirm failure It can not find the record or the request is not for you confirm.");
            return;
        }
    }

	public function friendslistAction(){
		
		$friendsMapper = new Model_FriendsMapper();
		//get user id from session.
       	$session = $this->_session;
        $userId = $session->user_id;
        //get confirmed friends in the database.
		$friendsList = $friendsMapper->findFriendsListByUserid($userId);
		//transfer the friend list to view.
        $this->view->friendsList = $friendsList;
        
	}
	
	public function unconfirmlistAction(){
		
		$friendsMapper = new Model_FriendsMapper();
		
		$storage = new Zend_Auth_Storage_Session();
       	$session = $storage->read();
        $userId = $session->user_id;
		$friendsList = $friendsMapper->findUnconfirmListByUserId($userId);
		
		$r=array();
        foreach ($friendsList as $s){
            $t=array (
                "friend_id" => $s->getFriend_id(),
            	"foudner_id" => $s->getFounder_id(),
                "founder_name" => $s->getFounder_name()
            );
            $r[]=$t;
        }
		//return view with json.
        $this->_helper->json($r);
	}
	
	public function rejectFriendsAction(){
	    
		$friendsMapper = new Model_FriendsMapper();
        $friends = new Model_Friends();
    	
        $storage = new Zend_Auth_Storage_Session();
       	$session = $storage->read();
        $userId = $session->user_id;
        
        //get the data id from view.
        $friendId = $_REQUEST['friend_id'];
        
    	//get the record by id and check if the user have permission to confirm
        $friendsMapper->find($friendId, $friends);
        if($userId == $friends->getTarget_id()){
        	$friends->setStatues("J");
        	try{
	            $friendsMapper->save($friends);
	            $this->_helper->json("You have reject the request.");
	            return;
        	} catch (Exception $e){
        		$this->_helper->json("Reject operation failed.");
	            return;
        	}
        } else {
            $this->_helper->json("Confirm failure It coan not find the record or the request is not for you confirm.");
            return;
        }
	}
}





